package com.kaili.basic.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.kaili.basic.pojo.dto.MdmDatadictDto;
import com.kaili.basic.pojo.entity.MdmDatadictEntity;
import com.kaili.basic.pojo.vo.MdmDatadictVo;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.exception.RestException;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.util.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.kaili.basic.service.IDatadictService ;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.UUID;


/**
* 数据字典 控制器
* @author:liuyang
* @Time: 2019-07-23
* @Copyright: ©  杭州凯立通信有限公司 版权所有
* @Warning: 本内容仅限于公司内部传阅,禁止外泄或用于其它商业目的
*/
@ApiVersion(1)
@RequestMapping("/{api_version}")
@RestController
@Api(tags = "数据字典管理", description = "数据字典管理相关设置API")
public class MdmDatadictController {
    @Autowired
    IDatadictService datadictService;

	@GetMapping(value = "/datadicts/{id}")
	@ApiOperation("根据ID查询数据字典")
	public MdmDatadictVo queryById(@ApiParam("ID") @PathVariable("id") Integer id) {
		return datadictService.queryById(id);
	}

	@GetMapping(value = "/datadicts")
	@ApiOperation("左侧数据字典父节点列表查询 or 子数据字典列表查询")
	public List<MdmDatadictVo> queryList(@ModelAttribute MdmDatadictDto dto) {
		return datadictService.queryList(dto);
	}

    @PostMapping(value = "/datadicts")
    @ApiOperation("新增数据字典")
    public RestStatus add(@RequestBody MdmDatadictEntity entity) {
		Wrapper<MdmDatadictEntity> wrapper = new EntityWrapper<>();
		wrapper.where("datadict_name={0}", entity.getDatadictName());
		List<Object> list = datadictService.selectObjs(wrapper);
		if(list!=null && list.size()>0){
			return CommonStatus.ACC_ADD_FAIL;
		}
		entity.setDatadictNameZh(entity.getDatadictName());
		entity.setDatadictNameEn(entity.getDatadictName());
        datadictService.insert(entity);

        return CommonStatus.ADD_OK;
    }

    @PutMapping(value = "/datadicts/{id}")
    @ApiOperation("修改数据字典")
    public RestStatus update(@RequestBody MdmDatadictEntity entity) {
		Wrapper<MdmDatadictEntity> wrapper = new EntityWrapper<>();
		wrapper.where("datadict_name={0} and id!={1}", entity.getDatadictName(),entity.getId());
		List<Object> list = datadictService.selectObjs(wrapper);
		if(list!=null && list.size()>0){
			return CommonStatus.ACC_ADD_FAIL;
		}
		entity.setDatadictNameZh(entity.getDatadictName());
		entity.setDatadictNameEn(entity.getDatadictName());
		datadictService.updateById(entity);

        return CommonStatus.UPDATE_OK;
    }

    @DeleteMapping(value = "/datadicts/{id}")
    @ApiOperation("删除数据字典")
    public RestStatus del(@ApiParam("数据字典ID") @PathVariable("id") Integer id) {
		MdmDatadictEntity entity= new MdmDatadictEntity();
		entity.setId(id);
		entity.setRdStatus("0");
		datadictService.updateById(entity);

        return CommonStatus.DELETE_OK;
    }

	@GetMapping(value = "/datadicts/export")
	@ApiOperation("数据字典导出")
	public void export(HttpServletResponse response, @ModelAttribute MdmDatadictDto dto) throws RestException {
		List<MdmDatadictVo> list = datadictService.queryList(dto);
		ExcelUtil.exportExcel(list,"数据字典","sheet1",MdmDatadictVo.class, UUID.randomUUID().toString()+".xls",response);
	}
}
